package com.wu.other.lazyQueue;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.wu.rabbitmq.utils.RabbitMQUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-12 17:42
 */
public class Producer {
    /**
     * 队列名称  priority-queue
     */
    private static final String QUEUE_NAME = "lazy-queue";

    /**
     * 发消息
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        Map<String, Object> arguments = new HashMap<>();
        //优先级官方允许 0-255，我们现在设置为 0-10，不要设置过大，会浪费内存
        arguments.put("x-queue-mode", "lazy");
        channel.queueDeclare(QUEUE_NAME, true, false, false, arguments);
        for (int i = 1; i <= 10; i++) {
            String message = "info-" + i;
            if (i == 5) {
                AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(5).build();
                channel.basicPublish("", QUEUE_NAME, properties, message.getBytes());
            } else {
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            }
        }
        System.out.println("消息发送完毕");
    }
}
